/*
 * mathutil.h
 *
 *  Created on: Jun 23, 2009
 *  	Author: matt
 */

#ifndef MATHUTIL_H_
#define MATHUTIL_H_
#include <stdio.h>
#include <math.h>
//! an abstract vector
typedef struct tagVECTOR2DF
{
	float x, y;
} VECTOR2DF;

typedef struct tagPOLARCOORD2DF
{
	float r;
	float theta;
} POLARDCOORD2DF;

float dist( VECTOR2DF const,  VECTOR2DF const);

//! an abstract line
class LINE2DF
{
public:
	VECTOR2DF slope;
	VECTOR2DF origin;
	float length;

	LINE2DF(){
		//does nothing
	}

	LINE2DF( VECTOR2DF s, VECTOR2DF o, float l){
		slope = s;
		origin = o;
		length = l;
		normalize();
	}

	LINE2DF( VECTOR2DF e1, VECTOR2DF e2){
			length = dist(e1, e2);
			origin.x = e1.x;
			origin.y = e1.y;
			slope.x = e2.x - e1.x;
			slope.y = e2.y - e1.y;
			normalize();
	}

	void normalize();

	bool intersect( LINE2DF const, VECTOR2DF&);

};

/*! determines the closest point on a line to a given point
 *  @param VECTOR2DF the given point
 *  @param LINE2DF the given line
 *  @param VECTOR2DF the point on the line that is closest to the given point
 *  @return bool false if there was a problem with the calculation, true otherwise
 */
bool closest_pnt_on_line( VECTOR2DF const,  LINE2DF const, VECTOR2DF&);


void endpoints( LINE2DF const, VECTOR2DF&, VECTOR2DF&);

//obsolete
//void line(const VECTOR2DF, const VECTOR2DF, LINE2DF&);

bool collision( LINE2DF const,  LINE2DF const);

float angle( VECTOR2DF const,  VECTOR2DF const);

float dot(VECTOR2DF const,  VECTOR2DF const);

void polar_to_cartesian(POLARDCOORD2DF const, VECTOR2DF& );

void cartesian_to_polar(VECTOR2DF const, POLARDCOORD2DF&);

void normalize(VECTOR2DF & v);

#endif /* MATHUTIL_H_ */
